package com.customers.controller;

import com.alibaba.fastjson2.JSON;
import com.customers.entity.Customer;
import com.customers.entity.MainForm;
import com.customers.service.CustomerService;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/FormUpdate")
public class FormServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mainFormJson = request.getParameter("Mainform");
        System.out.println("mainFormJson:"+mainFormJson);

        String customerJson = request.getParameter("Customer");
        System.out.println("customerJson:"+customerJson);
        ObjectMapper objectMapper = new ObjectMapper();
        MainForm mainform = objectMapper.readValue(mainFormJson, MainForm.class);

        List<Customer> customers = JSON.parseArray(customerJson,Customer.class);
        try {
            CustomerService customerService = (CustomerService) request.getServletContext().getAttribute("CustomerService");
            String customerSql = "customerIns";
            String contactSql = "contactIns";
            String getIdSql = "customerByName";
            customerService.MainFormSubmit(mainform,customers,customerSql,contactSql,getIdSql);

            Integer id = null;
            id = customerService.getCustomerId(mainform,getIdSql);
            response.getWriter().print(id);
//            CustomerServic.MainFormSubmit(mainform, customers);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
